package it.unibz.easytravel.expenses;

import java.util.Vector;

import it.unibz.easytravel.common.Person;
import it.unibz.easytravel.db.TravellersDbAdapter;
import it.unibz.easytravel.expenses.statistic.Converter;
import it.unibz.easytravel.travel.EditTravel;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.*;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

/**
 * This Activity class displays the data stored in TravelersDatabas in form of column bars
 * which represents amount of money flow Traveler is in debt or in credit at the moment in this travel
 *
 */
public class StatisticView1 extends Activity{

	protected static TravellersDbAdapter mTravelersDb;
	protected static Activity act;
	private static int travel_id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SharedPreferences settings = getSharedPreferences(EditTravel.PREFS_NAME, 0);
		travel_id = settings.getInt(EditTravel.TRAVEL_ID, -1);
		
		this.act = this;
		
		mTravelersDb = new TravellersDbAdapter(this);
		mTravelersDb.open();

		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {
		private Paint mPaint;

		public SampleView(Context context) {
			super(context);
			setFocusable(true);

			mPaint = new Paint();
			mPaint.setAntiAlias(true);
		}

		@Override protected void onDraw(Canvas canvas) {
			int width = canvas.getWidth();
			int height = canvas.getHeight()-110;
			int maxDeviation = 0;
			//get the data that must be drawn in the graph
			Cursor travelersCursor = mTravelersDb.getTravellers(travel_id);
			Vector<Person> travelers= new Vector<Person>();
			int moneyFlow, id;
			int nr_of_travelers=0;
			while(travelersCursor.moveToNext()){
				moneyFlow = travelersCursor.getInt(travelersCursor.getColumnIndexOrThrow(TravellersDbAdapter.MONEY_FLOW));
				id = travelersCursor.getInt(travelersCursor.getColumnIndexOrThrow(TravellersDbAdapter.PERSON_ID));
				travelers.addElement(new Person(id, moneyFlow, act));
				if(Math.abs(moneyFlow) > maxDeviation) maxDeviation = Math.abs(moneyFlow);
				if(moneyFlow != 0) nr_of_travelers++;
			}

			
			//compute coordinates
			float barWidth = 25;
			float spaceWidth = 15;
			float startPointY = 30;
			float maxBarHeight = 100;
			float textOffset = 10;
			float centerPointX = width/2;
			float graphHeight = barWidth*nr_of_travelers + spaceWidth*(nr_of_travelers-1);

			//graph would not fit into screen. Recalculate bar Width and space width
			if(graphHeight+2*startPointY > height){
				float spaceForBars =  (height-2*startPointY)/nr_of_travelers;
				barWidth = spaceForBars*2/3;
				spaceWidth = spaceForBars/3;
				graphHeight = barWidth*nr_of_travelers + spaceWidth*(nr_of_travelers-1);
			}

			//draw central line
			if(nr_of_travelers>0){
			mPaint.setARGB(255, 200, 200, 200);
			canvas.drawLine(centerPointX,
					startPointY-spaceWidth, 
					centerPointX,
					startPointY+graphHeight+spaceWidth,
					mPaint);
			}else{
				mPaint.setARGB(255, 255, 255, 255);
				mPaint.setTextAlign(Align.CENTER);
				canvas.drawText("Nobody is in debt on this Travel", centerPointX, startPointY, mPaint);
				
			}
			
			//draw columns
			Person traveler;
			nr_of_travelers = travelers.size();
			for (int i = 0; i < nr_of_travelers; i++) {
				traveler = travelers.elementAt(i);
				
				//draw column
				moneyFlow = traveler.getMoney();
				//positive column
				if(moneyFlow > 0){
					mPaint.setARGB(255, 0, 191, 24);
					canvas.drawRect(centerPointX-maxBarHeight*moneyFlow/maxDeviation,
							startPointY,
							centerPointX,
							startPointY + barWidth,
							mPaint);
						
					//write text
					mPaint.setARGB(255, 255, 255, 255);
					mPaint.setTextAlign(Align.LEFT);
					canvas.drawText(traveler.getName()+"  +"+Converter.convertToStringMerge(traveler.getMoney()), centerPointX+textOffset, startPointY+barWidth*2/3, mPaint);
					
					//increase the start point position on X-axis
					startPointY += barWidth + spaceWidth;
				}
				//negative column
				if(moneyFlow < 0){
					mPaint.setARGB(255, 204, 0, 0);
					canvas.drawRect(centerPointX,
							startPointY,
							centerPointX+maxBarHeight*moneyFlow/-maxDeviation,
							startPointY + barWidth,
							mPaint);
					
					//write text
					mPaint.setARGB(255, 255, 255, 255);
					mPaint.setTextAlign(Align.RIGHT);
					canvas.drawText(traveler.getName()+"  "+Converter.convertToStringMerge(traveler.getMoney()), centerPointX-textOffset, startPointY+barWidth*2/3, mPaint);
					
					//increase the start point position on X-axis
					startPointY += barWidth + spaceWidth;
				}
			}
			
			canvas.restore();
		}
	}
}
